package de.hwrBerlin.dataMining.view.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import de.hwrBerlin.dataMining.constants.Images;
import de.hwrBerlin.dataMining.constants.UIConstants;
import de.hwrBerlin.dataMining.dataStructure.TreeNode;
import de.hwrBerlin.dataMining.view.TreePainter;
import de.hwrBerlin.dataMining.view.TreeZoomView;

public class TreeExportZoom extends Dialog {
    
    private boolean _save = false;


    private Shell _shell;
    private Display _display;
    public TreeExportZoom(Shell parent, int style) {
        super(parent, style);
    }

    public TreePainter open(TreeNode pTree) {

        final Shell parent = getParent();

        _shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.CLOSE | SWT.RESIZE);
        _shell.setText(UIConstants.EXPORT_ZOOM);
        _shell.setImages(Images.LOGO_ICONS);
        _shell.setLayout(new GridLayout(1, true));
        
        _display = _shell.getDisplay();
        
        SashForm sashForm = new SashForm(_shell, SWT.NONE);
        sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        
        TreeZoomView treeZoomView = new TreeZoomView(sashForm, SWT.NONE, null);
        final TreePainter treePainter = treeZoomView.getTreePainter();
        treePainter.repaint(pTree);
        
        Button button = new Button(_shell, SWT.PUSH);
        button.setText(UIConstants.EXPORT_ZOOM_SAVE);
        button.setToolTipText(UIConstants.EXPORT_ZOOM_SAVE_TOOLTIP);
        button.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
        button.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent pE) {
                treePainter.setParent(parent);
                _shell.dispose();
                _save = true;
            }
        });

        
        _shell.pack();
        _shell.setSize(1000, 600);
        _shell.open();
        
        while (!_shell.isDisposed()) {
            if (!_display.readAndDispatch()) {
                _display.sleep();
            }
        }
        
        if(_save){
            return treePainter;
        }
        return null;
    }

}